package com.qinghe.plant.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qinghe.plant.util.CodeType;
import com.qinghe.plant.util.Response;
import com.qinghe.plant.dao.User;
import com.qinghe.plant.model.UserReponsitory;

@RestController
public class UserController {

	@Autowired
	private UserReponsitory userReponsitory;

	@RequestMapping(value = "doUserLogin", method = RequestMethod.POST)
	public Response UserLogin(HttpServletRequest request) {
		String account = request.getParameter("account");
		String password = request.getParameter("password");		
		User user=FindUser(account, password);
		if (user!= null) {
			return new Response(CodeType.Success,JSONObject.toJSONString(user),"操作成功");
		} else {
			return new Response(CodeType.Failed,null,"登陆失败！");
		}
	}
	
	@RequestMapping(value = "doReadUsers", method = RequestMethod.POST)
	public Response ReadUsers(HttpServletRequest request) {
		Integer identity =Integer.parseInt(request.getParameter("identity"));
		List<User> users= FindUsers(identity);
		return new Response(CodeType.Success,JSONArray.toJSONString(users),"操作成功");
	}
	
	@RequestMapping(value = "doReadUser", method = RequestMethod.POST)
	public Response ReadUser(HttpServletRequest request) {
		Integer identity =Integer.parseInt(request.getParameter("identity"));
		Integer id =Integer.parseInt(request.getParameter("id"));
		User user = FindUser(identity,id);
		return new Response(CodeType.Success,JSONObject.toJSONString(user),"操作成功");
	}

	@RequestMapping(value = "doDeleteUser", method = RequestMethod.POST)
	public Response DeleteUser(HttpServletRequest request) {
		String id =request.getParameter("id");
		userReponsitory.deleteById(id);
		return new Response(CodeType.Success,null,"操作成功");
	}
	
	@RequestMapping(value="doAlterUser",method=RequestMethod.POST)
	public Response AlterUser(HttpServletRequest request)
	{
		User user=JSONObject.parseObject(request.getParameter("user"), User.class);
		user= userReponsitory.save(user);
		return new Response(CodeType.Success,JSONArray.toJSONString(user),"操作成功");		
	}
	
	@RequestMapping(value="doInsertUser",method=RequestMethod.POST)
	public Response InsertUser(HttpServletRequest request)
	{
		if(HasUser(request.getParameter("account")))
		{
			return new Response(CodeType.Failed,null,"用户名已经存在！");
		}
		else 
		{
			User user=new User();
			user.setAccount(request.getParameter("account"));
			user.setPassword( request.getParameter("password"));
			user.setIdentity(Integer.parseInt(request.getParameter("identity")));
			user.setCreator(request.getParameter("creator"));			
			user.setTimer(new Date());
			user= userReponsitory.save(user);
			return new Response(CodeType.Success,JSONObject.toJSONString(user),"操作成功");
		}
	}
	
	private List<User> FindUsers(Integer identity) {
		@SuppressWarnings("serial")
		List<User> result = userReponsitory.findAll(new Specification<User>() {
			@Override
			public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				list.add(cb.equal(root.get("identity").as(Integer.class), identity));

				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}
		});
		return result;
	}	
	
	private User FindUser(String account, String password) {
		@SuppressWarnings("serial")
		List<User> result = userReponsitory.findAll(new Specification<User>() {
			@Override
			public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();

				list.add(cb.equal(root.get("account").as(String.class), account));
				list.add(cb.equal(root.get("password").as(String.class), password));
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}

		});

		return (result.size()!=0)?result.get(0):null;
	}	
	
	private User FindUser(Integer identity ,Integer id) {
		@SuppressWarnings("serial")
		List<User> result = userReponsitory.findAll(new Specification<User>() {
			@Override
			public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();

				list.add(cb.equal(root.get("identity").as(Integer.class), identity));
				list.add(cb.equal(root.get("id").as(Integer.class), id));
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}

		});

		return (result.size()!=0)?result.get(0):null;
	}	
	
	private boolean HasUser(String account) {
		@SuppressWarnings("serial")
		List<User> result = userReponsitory.findAll(new Specification<User>() {
			@Override
			public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				list.add(cb.equal(root.get("account").as(String.class), account));

				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}
		});

		return (result.size()!=0)?true:false;
	}	
}
